package com.thti.mecool.client.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.thti.mecool.client.activity.R;
import com.thti.mecool.client.activity.TaskdoneDetailActivity;
import com.thti.mecool.client.base.BaseActivity;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class MutilListAdapter extends BaseExpandableListAdapter {
	ArrayList<ArrayList<HashMap<String, String>>> taskList;
	ArrayList<String> listTag;
	int listCount;
	boolean IsSync,IsDataSyncEcho;
	Context mContext;
	ArrayList<ArrayList<Integer>> stat=null;
	public MutilListAdapter(
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag, Context context) {
		this.taskList = taskList;
		this.listTag = listTag;
		mContext = context;
	}

	public MutilListAdapter(
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag,ArrayList<ArrayList<Integer>> stat, Context context) {
		this.taskList = taskList;
		this.listTag = listTag;
		this.stat=stat;
		mContext = context;
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {

		return taskList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(mContext).inflate(
				R.layout.mecool_tasklist_item, null);
		TextView time = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_itemtime);
		TextView name = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_itemname);
		TextView addr = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_itemaddr);
		time.setText(taskList.get(groupPosition).get(childPosition).get("time"));
		name.setText(taskList.get(groupPosition).get(childPosition).get("name"));
		addr.setText(taskList.get(groupPosition).get(childPosition).get("addr"));
		EditText undone_reason=(EditText)convertView.findViewById(R.id.mecool_undone_reason);
		if(mContext.getClass().equals(TaskdoneDetailActivity.class)){
			undone_reason.setVisibility(0);
		}
		TextView SyncText=(TextView)convertView.findViewById(R.id.mecool_tasklist_itemsync);
		Button Syncbtn=(Button)LayoutInflater.from(mContext).inflate(R.layout.mecool_taskmanage, null).findViewById(R.id.mecool_SyncBtn);
		CheckBox taskCheck=(CheckBox)convertView.findViewById(R.id.mecool_tasklist_itemchecked);
		if(stat!=null&&stat.size()>0){
			if(stat.size()>groupPosition){
				if(stat.get(groupPosition).get(0)==0&&stat.get(groupPosition).get(1)==groupPosition&&stat.get(groupPosition).get(2)==childPosition){
					SyncText.setVisibility(View.VISIBLE);
					if(stat.get(groupPosition).get(0)==0&&!BaseActivity.IsSyncEcho){
						taskCheck.setVisibility(0);
					}else{
						taskCheck.setVisibility(8);
					}
				}
			}
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return taskList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return listTag.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return listTag.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(mContext).inflate(
				R.layout.mecool_tasklist_item_tag, null);
		TextView tag = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_item);
		tag.setText(listTag.get(groupPosition));
		if(taskList.get(groupPosition).get(0).get("isType")!=null){
			if(taskList.get(groupPosition).get(0).get("isType").equals("assignment")){
			tag.setBackgroundColor(Color.rgb(255, 255,204));
			tag.setTextColor(Color.DKGRAY);
			}
		}else{
			tag.setBackgroundColor(Color.LTGRAY);
			tag.setTextColor(Color.WHITE);
			tag.setEnabled(false);			
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	static void ViewHolder(){
		
	}
}
